package com.example.studentcompetitionplatform.service.impl;

import com.example.studentcompetitionplatform.controller.dto.PageDto;
import com.example.studentcompetitionplatform.entity.CompetitionArchives;
import com.example.studentcompetitionplatform.entity.Users;
import com.example.studentcompetitionplatform.mapper.DangAnMapper;
import com.example.studentcompetitionplatform.service.IDangAnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DangAnService implements IDangAnService {
    @Resource
    private DangAnMapper dangAnMapper;
    @Override
    public void register(CompetitionArchives competitionArchives) {
        CompetitionArchives competitionArchives1=new CompetitionArchives();
        competitionArchives1.setUserId(competitionArchives.getUserId());
        competitionArchives1.setCompetitionId(competitionArchives.getCompetitionId());
        if (dangAnMapper.selectCP(competitionArchives1)!=null){
            System.out.println("已经保存了");
            return;
        }
        Date date=new Date();
        competitionArchives.setArchiveDate(date);
        dangAnMapper.insertCA(competitionArchives);
    }

    @Override
    public void delete(CompetitionArchives competitionArchives) {
        CompetitionArchives competitionArchives1=new CompetitionArchives();
        competitionArchives1.setArchiveId(competitionArchives.getArchiveId());
        dangAnMapper.delete(competitionArchives1);
    }

    @Override
    public void update(CompetitionArchives competitionArchives) {

    }

    @Override
    public Map<String, Object> page(Integer pageNum, Integer pageSize, Integer archiveId, Integer competitionId, Integer userId) {
        pageNum = (pageNum - 1) * pageSize;
        if (archiveId == 0) {
            archiveId = null;
        }
        if (competitionId == 0) {
            competitionId = null;
        }
        if (userId == 0) {
            userId = null;
        }
        List<CompetitionArchives> data = dangAnMapper.selectPage(pageNum, pageSize, archiveId, competitionId, userId);
        Integer total = dangAnMapper.findArchiveTotal(archiveId);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
